<?php
namespace App\Http\Controllers\Api;

use App\Http\Controllers\Controller;
use App\Http\Requests;
use Illuminate\Http\Request;
use App\Model\VideoCategory;
use App\Model\Video;
use App\Model\PlayAddress;
use JWTAuth;
use DB;


class VideoApiController extends Controller
{

    public function __construct()
    {
        parent::__construct();
        view()->share([
            '_xApi' => 'am-in',
        ]);
    }


    /**
     * @return array
     */
    function getVideoCategoryList()
    {

        $data = array();
        $category = VideoCategory::select('name')->where(['is_show'=>1])->get();


        if ($category->isEmpty()) {
            $data['info'] = '获取数据失败';
            $data['status'] = false;
            return $data;
        }

        /*foreach ($category->toArray() as $key => $value) {
            $result[] = $value;

            $result[$key]['children_name'] = array_column($value['children_api'], 'name');
            if(empty($result[$key]['children_name'])){
                unset($result[$key]);
            }
            unset($result[$key]['children_api']);
            unset($result[$key]['id']);
        }*/

        $data['status'] = true;
        $data['info'] = '成功获取信息';
        $data['data'] = $category;
        return $data;

    }

    function getVideoList()
    {

        $data = array();
        $videos = Video::with(['video_url' => function ($query) {
            $query->select('url', 'video_id');
        }])->with(['video_category' => function ($query) {
            $query->select('id', 'name');
        }])->select('id', 'video_category_id', 'name', 'thumb', 'title', 'stars','genre', 'desc')
            ->where('is_show', true)
            ->orderBy('is_top', "desc")
            ->orderBy('is_hot', "desc")
            ->orderBy('is_new', "desc")
            ->orderBy('is_recommend', "desc")
            ->orderBy('created_at')
            ->get();
        //return $videos;
        if ($videos->isEmpty()) {
            $data['info'] = '获取数据失败';
            $data['status'] = false;
            return $data;
        }

        $data['status'] = true;
        $data['info'] = '成功获取详情';

        foreach ($videos->toArray() as $key => $value) {
            $result[] = $value;
            $result[$key]['video_urls'] = array_column($value['video_url'], 'url');
            $result[$key]['category_name'] = $value['video_category']['name'];
            $id = Video::select('video_category_id')->where('id', $result[$key]['id'])->get()->toArray();

            /*foreach ($id as $k => $v) {
                $name = VideoCategory::where('id', $v)->get();
                //$ii = VideoCategory::select('name')->where('id', $name[$k]['parent_id'])->get();
                //$result[$key]['type'] = $ii[$k]['name'];
            }*/

            unset($result[$key]['video_url']);
            unset($result[$key]['video_category']);
            unset($result[$key]['video_category_id']);

        }
        $data['data'] = $result;
        return $data;


    }

    /**
     * 获取视频api
     * @param Request $request
     * @return array
     */
    public function setVideoAdd(Request $request)
    {

        $data = array();
        //&& !$request->thumb && !$request->title && !$request->stars && !$request->desc && !$request->video_urls && !$request->category_name

        if (!$request->name || !$request->category_name) {
            $data['info'] = '请输入请求参数';
            $data['status'] = false;
            return view('Admin.xHotel.api.setVideoAdd')->with(['data' => $data, '_api_video_add' => 'am-active']);
        }

        if (!empty($request->all())) {
            $data = $request->except(['video_urls', 'token', 'category_name']);
            if ($request->category_name) {
                $video_like = VideoCategory::where('parent_id', '!=', 0)->where('name', 'like', '%' . $request->category_name . '%')->get()->toArray();
                if (empty($video_like)) {
                    $video_categories = VideoCategory::where('parent_id', '!=', 0)->get()->toArray();
                    $data['video_category_id'] = $video_categories['0']['id'];
                } else {
                    $data['video_category_id'] = $video_like[0]['id'];
                };
            };

            $video = Video::create($data);

            if ($request->has('video_urls')) {
                foreach ($request->video_urls as $url) {
                    $video->video_url()->create(['url' => $url]);
                }
            }

            if (!$video->save()) {
                $data['info'] = '添加数据失败';
                $data['status'] = false;
                return $data;
            }

            $data['status'] = true;
            $data['info'] = '添加数据成功';
            return view('Admin.xHotel.api.setVideoAdd')->with(['data' => $data, '_api_device' => 'am-active']);
        }

    }

    /**
     * 播放地址
     * @param Request $request
     * @return array
     */
    public function setPlayAddress(Request $request){

        $data = array();
        if(!$request->source){
            $data['info'] = '请输入请求参数';
            $data['status'] = false;
            return view('Admin.xHotel.api.setPlayAddress')->with(['data' => $data,'_api_play_address' => 'am-active']);
        }

        $play_address = PlayAddress::where('source', $request->source)->select('url','type')->first();

        if($play_address){
            $data['status'] = true;
            $data['info'] = '成功获取信息';
            $data['data']=$play_address;
        }

        return $data;

    }

    public function setDemand(Request $request){

        $data = array();
        if (!$request->id) {
            $data['info'] = '请输入请求参数';
            $data['status'] = false;
            return view('Admin.xHotel.api.getApp')->with(['data' => $data, '_api_app' => 'am-active']);
        }
        Video::where('id',$request->id)->increment('num');
    }

}
